//package com.geekerp.authorization_server.sms;
//import com.geekerp.authorization_server.service.SmsUserService;
//import com.geekerp.authorization_server.support.RedisOperator;
//import org.springframework.security.authentication.AuthenticationProvider;
//import org.springframework.security.authentication.BadCredentialsException;
//import org.springframework.security.core.Authentication;
//import org.springframework.security.core.AuthenticationException;
//import org.springframework.security.core.userdetails.UserDetails;
//import org.springframework.security.core.userdetails.UsernameNotFoundException;
//import org.springframework.stereotype.Component;
//import org.springframework.util.StringUtils;
//
//@Component
//public class SmsCodeAuthenticationProvider implements AuthenticationProvider {
//    private final SmsUserService userDetailsService;
//    private final RedisOperator redisOperator;
//
//    public SmsCodeAuthenticationProvider(SmsUserService userDetailsService, RedisOperator redisOperator) {
//        this.userDetailsService = userDetailsService;
//        this.redisOperator = redisOperator;
//    }
//
//    @Override
//    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
//        String phoneNo = (String) authentication.getPrincipal();
//        String smsCode = (String) authentication.getCredentials();
//
//        if (!StringUtils.hasText(phoneNo) || !StringUtils.hasText(smsCode)) {
//            throw new BadCredentialsException("Phone number or SMS code cannot be empty");
//        }
//
//        UserDetails userDetails;
//        try {
//            userDetails = userDetailsService.loadUserByPhoneNum(phoneNo);
//        } catch (UsernameNotFoundException e) {
//            throw new BadCredentialsException("Invalid phone number or SMS code");
//        }
//
//        String storedSmsCode = (String) redisOperator.get("mobile_phone:" + phoneNo);
//        if (storedSmsCode == null || !storedSmsCode.equals(smsCode)) {
//            throw new BadCredentialsException("Invalid phone number or SMS code");
////        }
//
//        return new SmsCodeAuthenticationToken(userDetails.getAuthorities(), phoneNo, smsCode);
//    }
//
//    @Override
//    public boolean supports(Class<?> authentication) {
//        return SmsCodeAuthenticationToken.class.isAssignableFrom(authentication);
//    }
//}